//
//  ScanQRCodeViewController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/5/5.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit
import AVFoundation

class ScanQRCodeViewController: UIViewController {

    @IBOutlet weak var scanBackView: UIView!
    @IBOutlet weak var toBottom: NSLayoutConstraint!
    @IBOutlet weak var animationView: UIImageView!
    @IBOutlet weak var showMessageLabel: UILabel!
    
    var session : AVCaptureSession?
    weak var layer : AVCaptureVideoPreviewLayer?
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        satrtScanAnimation()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        startScan()
    }

    func startScan() {
        
        TWQRCodeTool.shareInstance.setRectInterest(orignRect: scanBackView.frame)
        TWQRCodeTool.shareInstance.scanQRCode(inView: view, isDrawFrame: true) { (result) in
            print(result.description)
        }
    }
}

extension ScanQRCodeViewController {
    // 扫描动画开启
    func satrtScanAnimation(){
        // 修改位置
        toBottom.constant = scanBackView.frame.size.height
        // 刷新下
        view.layoutIfNeeded()
        
        toBottom.constant = -scanBackView.frame.size.height
        UIView.animate(withDuration: 1.0) {
            UIView.setAnimationRepeatCount(MAXFLOAT)
            self.view.layoutIfNeeded()
        }
    }
    // 扫描动画关闭
    fileprivate func removeScanAnimation(){
        animationView.layer.removeAllAnimations()
    }
}

